package tp.pr4;

import tp.pr4.instructions.*;
import tp.pr4.instructions.exceptions.WrongInstructionFormatException;

/**
 * The interpreter is in charge of converting the user input in an instruction 
 * for the robot. Up to now, the valid instructions are:
 *   MOVE,
 *   TURN { LEFT | RIGHT },
 *   PICK <ITEM>,
 *   SCAN [ <ITEM> ],
 *   OPERATE <ITEM>,
 *   RADAR,
 *   DROP <ITEM>,
 *   HELP,
 *   QUIT
 * @author Alvaro Saez Hernando
 * @author Noel Clemente Montero 
 */
public class Interpreter {
	
	//private static final String LINE_SEPARATOR = System.lineSeparator();
	 static private Instruction[] ins ={
		new DropInstruction(),
		new HelpInstruction(),
		new MoveInstruction(),
		new OperateInstruction(),
		new PickInstruction(),
		new QuitInstruction(),
		new RadarInstruction(),
		new ScanInstruction(),
		new TurnInstruction()
	};
	
	/**
	 * Default constructor.
	 */
	public Interpreter(){

	}
	
	/**
	 * Generates a new instruction according to the user input
	 * @param line A string with the user input
	 * @return The instruction read from the given line. If the instruction is not correct, 
	 * then it throws an exception.
	 * @throws WrongInstructionFormatException
	 */
	public static Instruction generateInstruction(String line)throws WrongInstructionFormatException{
		for(Instruction c: ins){
			try{
				return c.parse(line);
			}catch (Exception e){}
		}
		throw new WrongInstructionFormatException("WALL·E says: I do not understand. Please repeat");
	}
	
	/**
	 * It returns information about all the instructions that the robot understands
	 * @return A string with the information about all the available instructions
	 */
	public static String interpreterHelp(){
		String help = "";
		for (Instruction c: ins){ 
			help += c.getHelp();
		}
		return help;
	}
	
}

